package com.chuji.crmcj.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.ListViewAdapter;
import com.chuji.crmcj.bean.CarConfigureInfo;
import com.chuji.crmcj.bean.CarTypeInfo;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.List;

public class TurnoverTypeActivity extends Activity {
    //汽车类型数据
    String [] typeForms={"菲翔","奔驰","宝马","玛莎拉蒂","保时捷"};
    //汽车类型网络存储
    List<CarTypeInfo.ApiParamObjEntity> carTypeData;
    //汽车类型数据
    private CarTypeInfo carTypeInfo;
    //返回
    private ImageView mIv_back;
    //显示
    private ListView mLv_car_type;
    //适配器
    CustomAdapter adapter;
    //请求码
    private int resultCode = 0;
    //公司ID
    String CompanyID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_turnover_type);
        //初始化组件
        mIv_back = (ImageView) findViewById(R.id.iv_back);
        mLv_car_type = (ListView) findViewById(R.id.lv_car_type);
        getTypeData();
    }

    private void getTypeData() {
        //第一次
        if (carTypeData == null) {
            carTypeData = new ArrayList<CarTypeInfo.ApiParamObjEntity>();
        }
        requestData();
    }
    private void requestData() {
        //初始化Volley
        //得到战败ID和用户ID
//        CompanyID= SPUtils.getString(UIUtils.getContext(), "CompanyID", "");
        CompanyID="C04B3AE6-1866-4490-9D73-FAB0DB121F12";
        String strURL = String.format(UrlUtils.GET_CAR_TYPE+"&CompanyID="+CompanyID);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                carTypeInfo = JSON.parseObject(response, CarTypeInfo.class);
                carTypeData.addAll(carTypeInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        //初始化适配器
                        adapter=new CustomAdapter();
                        //填充
                        mLv_car_type.setAdapter(adapter);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }

    //自定义适配器
    class CustomAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return carTypeData.size();
        }

        @Override
        public Object getItem(int position) {
            return carTypeData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            /****1)根据list_item.xml--》得到当前子项元素的布局   【界面】*******/
            //b)声明ViewHolder
            ViewHolder viewHolder = null;
            //c)判断convertView是否为null
            if (convertView == null) {//第一次调用getView（）方法时为 null,意思就是在内存中没有
                viewHolder = new ViewHolder();
                convertView =  LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.listview_car_type, null);
                //初始化组件
                viewHolder.tv_car_type = (TextView) convertView.findViewById(R.id.tv_car_type);
//                viewHolder.iv_apply_tag = (ImageView) convertView.findViewById(R.id.iv_apply_tag);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv_car_type.setText(carTypeData.get(position).getCarModel());
            UIUtils.runInMainThread(new Runnable() {
                @Override
                public void run() {
                    mLv_car_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            //传值，返回数据
                            Intent mIntent = new Intent();
                            mIntent.putExtra("type",carTypeData.get(position).getCarModel());
                            // 设置结果，并进行传送
                            setResult(resultCode, mIntent);
                            finish();
                        }
                    });
                    mIv_back.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //传值，返回数据
                            Intent mIntent = new Intent();
                            mIntent.putExtra("type","未选择");
                            // 设置结果，并进行传送
                            setResult(resultCode, mIntent);
                            finish();
                        }
                    });
                }
            });
            return convertView;
        }
    }
    static class ViewHolder {
        private TextView tv_car_type;
    }
    //屏蔽系统返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) { //监控/拦截/屏蔽返回键

        }
        return false;
    }
}
